//
//  DDClientInfoManager.m
//  DingDongZP
//
//  Created by Jyh on 16/4/20.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDClientInfoManager.h"
#import "DDChangeView.h"

static NSString *kClientType = @"kClientType";

@implementation DDClientInfoManager

static DDClientType _clientType;

+ (void)initClientType:(DDClientType )clientType
{
    _clientType = clientType;
    
    [[NSUserDefaults standardUserDefaults] setInteger:_clientType forKey:kClientType];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (DDClientType )clientType
{
    if ([[NSUserDefaults standardUserDefaults] integerForKey:kClientType]) {
        
        if ([[NSUserDefaults standardUserDefaults] integerForKey:kClientType] == DDClientTypeCattle) {
            
            return DDClientTypeCattle;
        } else if ([[NSUserDefaults standardUserDefaults] integerForKey:kClientType] == DDClientTypeBoss) {
            
            return DDClientTypeBoss;
        } else {
            
            return DDClientTypeCattle;
        }
    } else {
//        NSLog(@"获取客户端状态失败，未设置");
        return DDClientTypeNone;
    }
    return _clientType;
}

+ (void) changeClientType:(UIViewController *)target andShowText:(NSString *)showText{
    
//    WEAKSELF
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        
        // 切换版本
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//        [[LDShowHUB shareInstance] showStaus:@"切换中，请稍候..."];
        DDChangeView *popChangeView = [[DDChangeView alloc]initWithCreatPopViewframe:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andShowText:showText];

        [[RCIM sharedRCIM] logout];
        
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            
            if ([DDClientInfoManager clientType] == DDClientTypeNone) {
                
                [DDClientInfoManager initClientType:DDClientTypeCattle];
            }
            
            if ([DDClientInfoManager clientType] == DDClientTypeCattle) {
                //设置版本
                [DDClientInfoManager initClientType:DDClientTypeBoss];
                [delegate initBossTabbarController];
                [[LDShowHUB shareInstance] showSucc:@"切换成功"];
                
            } else if ([DDClientInfoManager clientType] == DDClientTypeBoss) {
                
                [DDClientInfoManager initClientType:DDClientTypeCattle];
                [delegate initCattleTabbarController];
                [[LDShowHUB shareInstance] showSucc:@"切换成功"];
                
            } else {
                
                [[LDShowHUB shareInstance] showErr:@"切换客户端错误"];
//                NSLog(@"MeController切换客户端错误~~~");
            }
        });
    }];
    
}

@end
